package com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeAddDevices.persenter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.example.anew.journey.mynewjourney.base.MyApplication;
import com.example.anew.journey.mynewjourney.bean.ISSuccessMain;
import com.example.anew.journey.mynewjourney.bean.TakeDateBean;
import com.example.anew.journey.mynewjourney.config.RBConstants;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeAddDevices.bean.AddDeviceBean;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeAddDevices.model.FirstChangeAddDevicesModel;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeAddDevices.model.ISFirstChangeAddDevicesModel;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeAddDevices.view.ISFirstChangeAddDevicesView;
import com.example.anew.journey.mynewjourney.firstchangelogic.firstChangeCardiacDetection.FirstChangeCardiacDetectionActivity;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import org.greenrobot.eventbus.EventBus;

import cn.jesse.nativelogger.NLogger;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 杭州惟君医疗科技有限公司17
 * Created by 20433 on 2018/3/14
 */

public class FirstChangeAddDevicesPersenter  {

    private ISFirstChangeAddDevicesView mISFirstChangeAddDevicesView;
    private ISFirstChangeAddDevicesModel mISFirstChangeAddDevicesModel ;
    public FirstChangeAddDevicesPersenter(ISFirstChangeAddDevicesView iSFirstChangeAddDevicesView) {
        this.mISFirstChangeAddDevicesView = iSFirstChangeAddDevicesView;
        mISFirstChangeAddDevicesModel = new FirstChangeAddDevicesModel();
    }
    // 绑定设备
    public void  bandingDevics(){
        mISFirstChangeAddDevicesView.showToastInformation("绑定成功");
        //mISAddDeviecsView.showBindingFailure();
        // todo 访问后台绑定设备
        mISFirstChangeAddDevicesView.setBtNextContent("解除绑定");
    }
    // 解除绑定
    public void  removeBandingDevices(){
        mISFirstChangeAddDevicesView.showToastInformation("解除成功");
        // todo 访问后台解除绑定设备
        mISFirstChangeAddDevicesView.setBtNextContent("下一步");
    }
    // 下一步
    public void  onClickBtNext(){
        String str =  mISFirstChangeAddDevicesView.getBtNextContent();
        if(str.equals("下一步")){  //下一步的逻辑
            mISFirstChangeAddDevicesView.showInputBandingCode();
        }else if(str.equals("解除绑定")){ //解除绑定的逻辑
            mISFirstChangeAddDevicesView.showRemoveBinding();
        }
    }
    //输入绑定的码 取消
    public  void cancelInputBandingCode(){
        mISFirstChangeAddDevicesView.cancelInputBandingCode();
    }
    //输入绑定的  确认绑定
    public void inputBandingCodeConfirmBinding(Context mContext ,String devices_id){
        String inputBandingCode = mISFirstChangeAddDevicesView.getInputBandingCode();
        if(TextUtils.isEmpty(inputBandingCode)) {
            mISFirstChangeAddDevicesView.showToastInformation("请输入绑定码");
            return;
        }
        TakeDateBean bean = mISFirstChangeAddDevicesModel.TakeDate(mContext);
        if(bean == null) return;
        addDevice(bean.getUser_id(),inputBandingCode,devices_id,mContext);

    }


    //绑定失败 结束绑定
    public void bindingFailureEndBinding(Context mContext ,String devices_id){
        mISFirstChangeAddDevicesView.bindingFailureEndBinding();
    }
    //绑定失败 重新绑定
    public void bindingFailureRebind(Context mContext ,String devices_id){
        mISFirstChangeAddDevicesView.bindingFailureEndBinding();
        String inputBandingCode = mISFirstChangeAddDevicesView.getInputBandingCode();
        if(TextUtils.isEmpty(inputBandingCode)) {
            mISFirstChangeAddDevicesView.showToastInformation("请输入绑定码");
            return;
        }
        TakeDateBean bean = mISFirstChangeAddDevicesModel.TakeDate(mContext);
        if(bean == null) return;
        bindingFailureBinding(bean.getUser_id(),inputBandingCode,devices_id,mContext);



    }

    private void bindingFailureBinding(String user_id, String inputBandingCode, String devices_id, final Context mContext) {
        String url = RBConstants.SERVER_URL+RBConstants.ADD_DEVICES;
        OkHttpUtils
                .post()
                .url(url)
                .addParams("goods_id", devices_id)
                .addParams("cus_id", user_id)
                .addParams("binding_code", inputBandingCode)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String NetworkResponseDate = response.body().string();
                        Gson gson = new Gson();
                        NLogger.e("TAG","NetworkResponseDate======00000====="+NetworkResponseDate);
                        final AddDeviceBean addDeviceBean =    gson.fromJson(NetworkResponseDate, AddDeviceBean.class);
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(addDeviceBean.errorCode == 200){
                                    mISFirstChangeAddDevicesView.showToastInformation("绑定成功");
                                    String devicesName =  addDeviceBean.data;
                                    if(TextUtils.isEmpty(devicesName)){
                                        cancelInputBandingCode();
                                    }else{
                                        Intent intent = new Intent(mContext, FirstChangeCardiacDetectionActivity.class); //FirstChangeCardiacDetectionActivity
                                        intent.putExtra("devices_name",devicesName.trim());
                                        NLogger.e("TAG","NetworkResponseDate======00000====="+devicesName.trim());
                                        mContext.startActivity(intent);
                                        mISFirstChangeAddDevicesView.finshPager();
                                        EventBus.getDefault().post(new ISSuccessMain(true));
                                    }

                                }else{
                                    mISFirstChangeAddDevicesView.showToastInformation(addDeviceBean.errorMessage);
                                    mISFirstChangeAddDevicesView.bindingFailureEndBinding();
                                    mISFirstChangeAddDevicesView.showInputBandingCode();
                                }

                            }
                        });
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                       // mISFirstChangeAddDevicesView.showToastInformation("请查看网络");
                        cancelInputBandingCode();
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }



    //解除绑定 我在想想
    public void verifyBindingFailedIThink(){
        mISFirstChangeAddDevicesView.removeBindingIThink();
    }
    //解除绑定 确认解绑
    public void verifyBindingFailedConfirmUnbundling(){
        mISFirstChangeAddDevicesView.removeBindingConfirmUnbundling();
        removeBandingDevices();
    }

    public void onClickeCross(){
        mISFirstChangeAddDevicesView.endPage();
    }

    //点击条状到使用说明
   public void onClickDirectionsUse(){
       mISFirstChangeAddDevicesView.toDirectionsUsePager();
    }
    public void initDate(int Page_identification_code){
        if(Page_identification_code == 0){
            mISFirstChangeAddDevicesView.setBtNextContent("解除绑定");
        }else  if(Page_identification_code == 1){
            mISFirstChangeAddDevicesView.setBtNextContent("下一步");
        }
    }

    private void addDevice(String user_id, String inputBandingCode, String devices_id, final Context mContext) {
        String url = RBConstants.SERVER_URL+RBConstants.ADD_DEVICES;
        OkHttpUtils
                .post()
                .url(url)
                .addParams("goods_id", devices_id)
                .addParams("cus_id", user_id)
                .addParams("binding_code", inputBandingCode)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String NetworkResponseDate = response.body().string();
                        Gson gson = new Gson();
                       // NLogger.e("TAG","NetworkResponseDate======44444====="+NetworkResponseDate);
                        final AddDeviceBean addDeviceBean =    gson.fromJson(NetworkResponseDate, AddDeviceBean.class);
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(addDeviceBean.errorCode == 200){
                                    mISFirstChangeAddDevicesView.showToastInformation("绑定成功");
                                   String devicesName =  addDeviceBean.data;
                                    NLogger.e("TAG","NetworkResponseDate======44444====="+devicesName);
                                    if(TextUtils.isEmpty(devicesName)){
                                        cancelInputBandingCode();
                                    }else{
                                        Intent intent = new Intent(mContext, FirstChangeCardiacDetectionActivity.class); //FirstChangeCardiacDetectionActivity
                                        intent.putExtra("devices_name",devicesName.trim());
                                       // NLogger.e("TAG","NetworkResponseDate======00000====="+devicesName.trim());
                                        mContext.startActivity(intent);
                                        mISFirstChangeAddDevicesView.finshPager();
                                        EventBus.getDefault().post(new ISSuccessMain(true));
                                    }

                                }else{

                                    mISFirstChangeAddDevicesView.showToastInformation(addDeviceBean.errorMessage);
                                    cancelInputBandingCode();
                                    mISFirstChangeAddDevicesView.showBindingFailure();
                                }

                            }
                        });
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                       // NLogger.e("TAG","NetworkResponseDate======44444====="+e);
                       // mISFirstChangeAddDevicesView.showToastInformation("请查看网络");
                        cancelInputBandingCode();
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }
}
